<?php

namespace app\admin\controller;

use app\admin\model\Image;
use app\admin\model\Crop;
use app\admin\model\Croptype;
use app\admin\model\Medicament;

use think\Request;

class Harm extends Base
{

    public function index() {
        return parent::index();
    }

    public function add(Request $request) {
        // 农作物类目类
        $croptypeList = Croptype::order('sorting ASC')->select();
        $this->assign('croptypeList', $croptypeList);

        return parent::add($request);
    }

    public function addPost(Request $request, $redirect = '') {

        return parent::addPost($request, $this->redirect);
    }

    public function edit(Request $request) {
        $model = model($this->model);
        $id = $request->param('id');
        // Get data info
        $info = $model->find($id);

        if(!$info){
            return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
        }

        // 发生农作物
        $cropsList = Crop::whereIn('id', $info['crops'])->field('id, name')->select();
        $this->assign('cropsList', $cropsList);
        // 适用药剂
        $medicamentsList = Medicament::whereIn('id', $info['medicaments'])->field('id, name')->select();
        $this->assign('medicamentsList', $medicamentsList);

        // 农作物类目类
        $croptypeList = Croptype::order('sorting ASC')->select();
        $this->assign('croptypeList', $croptypeList);

        $imagesList = Image::where(['relatedId' => $request->param('id'), 'tag' => 'imglist'])->order('sorting ASC')->select();
        $this->assign('imgList', $imagesList);
        $this->assign('imgcount', count($imagesList));

        return $this->fetch($this->editView, [
            'info' => $info
        ]);
    }

    public function editPost(Request $request, $redirect = '') {
        return parent::editPost($request, $this->redirect);
    }

    public function delete() {
        return parent::delete();
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        return $where;
    }

    /**
     * 农作物列表
     */
    public function cropsList(){
        $request = $this->request;
        $param = $request->param();
        $model = new Crop();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];

        if(!$param['typeId']){
            $result = [
                'status' => '0',
                'draw' => $param['draw'],
                'data' => [],
                'recordsFiltered' => 0,
                'recordsTotal' => 0,
            ];
            return json($result);
        }

        $where = [
            'typeId' => $param['typeId'],
        ];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        $list = $model->where($where)->limit($start, $length)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    /**
     * 药剂列表
     */
    public function medicamentsList(){
        $request = $this->request;
        $param = $request->param();
        $model = new Medicament();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];

        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        $list = $model->where($where)->limit($start, $length)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

}
